package com.iplatform.iplatformuser.privilege.service.impl;

import com.iplatform.commons.utils.CommResponse;
import com.iplatform.commons.utils.UuidUtil;
import com.iplatform.iplatformuser.framework.util.UniqueIDGenerator;
import com.iplatform.iplatformuser.privilege.dao.PriManagerMapper;
import com.iplatform.iplatformuser.privilege.dao.PriMenuMapper;
import com.iplatform.iplatformuser.privilege.dao.PriRoleMapper;
import com.iplatform.iplatformuser.privilege.service.inter.IPriRoleService;
import com.iplatform.module.privilege.CompatibilityBean;
import com.iplatform.module.privilege.PriManagerBean;
import com.iplatform.module.privilege.PriMenuBean;
import com.iplatform.module.privilege.PriRoleBean;
import com.iplatform.module.privilege.group.PriGroupInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>Title: PriRoleServiceImpl</p>
 * <p>Description: </p>
 *
 * @author RayLLi
 * @date 2018年8月23日
 */
@Service
public class PriRoleServiceImpl implements IPriRoleService {

    @Autowired
    private PriRoleMapper userRoleMapper;

    @Autowired
    private PriManagerMapper priManagerMapper;

    @Autowired
    private PriMenuMapper priMenuMapper;

    @Override
    public List<PriRoleBean> findRoleByMrgId(String mgrId) {
		PriRoleBean priRole = new PriRoleBean();
		Map<String, String> formMap = new HashMap<String,String>(4);

		List<String> roleIds = userRoleMapper.findRoleIdByMrgId(mgrId);
		List<PriRoleBean> roleBeans = new ArrayList<>();
		if (roleIds.size() > 0){
		    for (String roleId : roleIds){
                formMap.put("ROLE_ID", roleId);
                priRole = userRoleMapper.queryRole(formMap);
                roleBeans.add(priRole);
            }
        }

		  return roleBeans;
    }

    @Override
    public List<PriRoleBean> loadRole(Map<String, String> formMap) {
        return userRoleMapper.loadRole(formMap);
    }

    @Override
    @Transactional
    public int insert(PriRoleBean priRoleBean) {
        return userRoleMapper.insert(priRoleBean);
    }

    @Transactional
    public int update(PriRoleBean priRoleBean) {
        return userRoleMapper.update(priRoleBean);
    }

    @Transactional
    public void delete(String roleId) {
        userRoleMapper.delete(roleId);
    }

    @Transactional
    public void batchDelete(String[] roleIds) {
        userRoleMapper.batchDelete(roleIds);
    }

    @Override
    @Transactional
    public void setIsUse(String roleId) {
        userRoleMapper.setIsUse(roleId);
    }

    @Override
    public List<PriManagerBean> querySelectManagerWithRoleId(Map<String, String> formMap) {
        return userRoleMapper.querySelectManagerWithRoleId(formMap);
    }

    @Override
    public List<PriManagerBean> querySelectManagerWithOutRoleId(Map<String, String> formMap) {
        return userRoleMapper.querySelectManagerWithOutRoleId(formMap);
    }

    @Override
    @Transactional
    public void updateManagerRole(String roleId, String checkedIds, String createdBy, boolean b) {

        if (!StringUtils.isEmpty(checkedIds)) {
            String[] mgrIds = checkedIds.split(",");
            List<Map<String, String>> list = new ArrayList<>();
            for (String id : mgrIds) {
                Map<String, String> mapData = new HashMap<>();
                mapData.put("USER_ROLE_ID", UniqueIDGenerator.getUUID());
                mapData.put("MGR_ID", id);
                mapData.put("ROLE_ID", roleId);
                mapData.put("CREATED_BY", createdBy);
                mapData.put("STATUS", "N");
                mapData.put("IS_DELETED", "N");
                list.add(mapData);
            }
//            if (b) {
//                //角色到此接口
//                userRoleMapper.delRoleManager(roleId);
//            } else if (!b) {
//                //用户到此接口
//                priManagerMapper.delManagerRoleId(mgrIds);
//            }

            priManagerMapper.delManagerRoleId(mgrIds);

            userRoleMapper.updateManagerRole(list);
        }
    }

    @Override
    @Transactional
    public void delManagerRole(String roleId, String checkedIds, String string) {
        String[] mgrIds = checkedIds.split(",");
        String mgrId = "";
        //拼接用户ID
        for (int i = 0; i < mgrIds.length; i++) {
            mgrId += "'" + mgrIds[i] + "'";
            if (i != mgrIds.length - 1) {
                mgrId += ",";
            }
        }
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put("roleId", roleId);
        formMap.put("mgrId", mgrId);
        userRoleMapper.delManagerRole(formMap);
//		//删除用户绑定的指标
//		userRoleMapper.delDefaultTargetAll(formMap);

    }

    @Transactional
    public void updateRoleMenuAndOpt(String roleId, String[] checkedMenuIds, String[] checkedOptIds, String createdBy) {

        // 更新用户可用的菜单
        userRoleMapper.deleteRoleMenu(roleId);
        if (checkedMenuIds != null) {
            for (String menuId : checkedMenuIds) {
                Map<String, String> formMap = new HashMap<String, String>(4);
                formMap.put("ROLE_MENU_ID", UniqueIDGenerator.getUUID());
                formMap.put("MENU_ID", menuId);
                formMap.put("ROLE_ID", roleId);
                formMap.put("CREATED_BY", createdBy);

                userRoleMapper.roleSelectedMenu(formMap);
            }
        }

        // 更新用户的可用操作
        userRoleMapper.deleteRoleOpt(roleId);
        if (checkedOptIds != null) {
            for (String optId : checkedOptIds) {
                Map<String, String> formMap = new HashMap<String, String>(4);
                formMap.put("ROLE_OPT_ID", UniqueIDGenerator.getUUID());
                formMap.put("MENU_OPT_ID", optId);
                formMap.put("ROLE_ID", roleId);
                formMap.put("CREATED_BY", createdBy);

                userRoleMapper.roleSelectedOpt(formMap);
            }
        }

    }

    @Override
    public List<Map<String, String>> queryAllOptByMenuIds(Map<String, String> formMap) {
        return userRoleMapper.queryAllOptByMenuIds(formMap);
    }

    private final String roleSelectedTarget = "Y";

    @Transactional
    public void selectTarget(String targetId, String roleId, String createdBy) {
        String isRoleSelectTarget = userRoleMapper.isRoleSelectTarget(roleId, targetId);

        if (roleSelectedTarget.equals(isRoleSelectTarget)) {
            userRoleMapper.deleteTarget(roleId, targetId);
        } else {
            Map<String, String> roleTargetMap = new HashMap<String, String>(4);
            roleTargetMap.put("ROLE_TARGET_ID", UniqueIDGenerator.getUUID());
            roleTargetMap.put("TARGET_ID", targetId);
            roleTargetMap.put("ROLE_ID", roleId);
            roleTargetMap.put("CREATED_BY", createdBy);

            userRoleMapper.selectTarget(roleTargetMap);
        }
    }

    public PriRoleMapper getUserRoleMapper() {
        return userRoleMapper;
    }

    public void setUserRoleMapper(PriRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    public List<CompatibilityBean> queryOrg() {
        return userRoleMapper.queryOrg();
    }

    @Override
    public List<PriGroupInfoBean> findGroupByRoleId(String roleId) {
        return userRoleMapper.findGroupByRoleId(roleId);
    }


    @Override
    public List<Map<String, String>> queryRoleName() {
        return userRoleMapper.queryRoleName();
    }

    @Override
    public List<Map<String, String>> querySelectTargetWithOutRoleId(Map<String, String> formMap) {
        return userRoleMapper.querySelectTargetWithOutRoleId(formMap);
    }

    @Override
    public List<Map<String, String>> querySelectTargetWithRoleId(Map<String, String> formMap) {
        return userRoleMapper.querySelectTargetWithRoleId(formMap);
    }

    @Override
    public void updateTargetRole(Map<String, String> formMap) {

        userRoleMapper.deleteTargetWithRoleId(formMap);

        if (!StringUtils.isEmpty(formMap.get("targetIds"))) {
            for (String targetId : formMap.get("targetIds").split(",")) {
                Map<String, String> map = new HashMap<>();
                map.put("targetId", targetId);
                map.put("roleId", formMap.get("roleId"));

                userRoleMapper.updateTargetRole(map);
            }
        }
    }

    @Override
    public List<PriMenuBean> queryMenuListWithRoleId(String roleId, String menuName) {
        List<PriMenuBean> currentMenuList = userRoleMapper.queryMenuListWithRoleId(roleId, menuName);

        return currentMenuList;
    }

    @Override
    public int addCheck(String roleCode, String roleId) {
        return userRoleMapper.addCheck(roleCode, roleId);
    }

    @Override
    public CommResponse roleBindMenus(String roleId, String[] menuIds, BigDecimal version) {
        userRoleMapper.romoveBind(roleId,null);
        //遍历菜单  并插入数据
        for (String menuId : menuIds) {
            userRoleMapper.RoleBindMenus(roleId, menuId, version);
        }
        return new CommResponse("绑定成功", true);
    }

    @Override
    public CommResponse removeBind(String roleId, String[] menuIds) {
        for (String menuId : menuIds) {
            userRoleMapper.romoveBind(roleId, menuId);
        }

        List<String> list = Arrays.asList(menuIds);
        String parentId = priMenuMapper.findParentId(list.get(0));
        Integer sonNum = priMenuMapper.findSonMenuNum(parentId,roleId);
        if (sonNum <= 0){
            userRoleMapper.romoveBind(roleId,parentId);
        }

        return new CommResponse("移除绑定成功", true);
    }

    @Override
    public void SynTarget(Map<String, String> formMap) {
        List<PriManagerBean> userList = userRoleMapper.querySelectManagerWithRoleId(formMap);
        List<Map<String, String>> targetList = userRoleMapper.querySelectTargetWithRoleId(formMap);
        for (PriManagerBean priManagerBean : userList) {
            formMap.put("mgrId", priManagerBean.getUserId());
            //角色绑定用户的默认指标
            userRoleMapper.delDefaultTarget(formMap);
            for (Map<String, String> map : targetList) {
                //重新同步默认指标
                formMap.put("targetId", map.get("id"));
                formMap.put("id", UuidUtil.genUUID());
                userRoleMapper.SynTarget(formMap);
            }
        }

    }


}
